/**************************************************************************
 * $Date$
 * $Author$
 * $Rev$
 * Copyright (c) 2019 Daiwa Institute of Research Business Innovation Ltd.
 *************************************************************************/
package com.headwind.multi.datasource.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/13
 * @description : 从数据源配置类
 */
@Log4j2
@Configuration
@EnableTransactionManagement
@MapperScan(value = {"com.headwind.mapper.read"}, sqlSessionFactoryRef = "db2SqlSessionFactory")
public class ReadDataSourceConfig {
    private final ResourceLoader resourceLoader;

    public ReadDataSourceConfig(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.read")
    public DataSource db2DataSource() {
        log.info("Read db create....");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        Resource[] resourceMappers = null;
        try {
            resourceMappers = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
                    .getResources("classpath:com/headwind/mapper/*.xml");
        } catch (Exception e) {
            log.warn(e.getMessage());
        }

        bean.setMapperLocations(resourceMappers);
        return bean.getObject();
    }

    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate db2SqlSessionTemplate(
            @Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
